Constructor
A constructor is a code block which will be called when the object is created. When the constructor is called the memory has been allocated to the object. This method is used to initialize the object. Whenever an object is created one of the constructors will be called. It is not compulsory that you have to create the constructor if you do not have any then java will create the default one.
Prerequisite for creating a constructor
- The constructor name should be the same as the name of the class.
- A constructor does not have any explicit return type.
- You cannot define a constructor as abstract, static, final.
Constructors are of two types- no-argument constructors and parameterized constructors.
Default constructor
If a constructor does not have the parameters then it is called default constructor. if there is no value passed then the destructor will display the default value of variable data type.
Syntax-
<class_name>()
{
//code
}
Example-
class Demo1{
Demo1()
{
System.out.println("Hello");
}
public static void main(String args[])
{
Demo1 b=new Demo1();
}
}
Output-
Hello
Parameterized constructor
When we create the constructor with a number of parameters then it is called a parameterized constructor. it will allow you to assign different values to distinct objects.
Example-
class Demo{
int id;
String name;
Demo(int i,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Demo s1 = new Demo(1,"Jacob");
Demo s2 = new Demo(2,"Sam");
s1.display();
s2.display();
}
}
Output-
1 Jacob
2 Sam
Constructor overloading
Java allows you to override the constructors just like methods. You can create more than one constructor with a different parameter list. Each of those constructors is created to perform different tasks. Which constructor to invoke that the object creation time will depend on the parameter list and their type.
Example-
class Demo{
int id;
String name;
Demo(int i,String n)
{
id = i;
name = n;
}
Demo (int id)
{
id=i;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Demo s1 = new Demo(1,"Jacob");
Demo s2 = new Demo(2);
s1.display();
s2.display();
}
}
Output-
1 Jacob
2 null
Copy constructor
Java does not exactly have copy constructor but allows you to copy the value of one object to another object just like C++. You can copy the object value to others using the below methods-
- Using constructor
- Assigning object’s value to another object
- Using clone() method of the object class.
Example using constructor
class Demo{
int id;
String name;
Demo(int i,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Demo s1 = new Demo(1,"Jacob");
Demo s2 = new Demo(s1);
s1.display();
s2.display();
}
}
Output-
1 Jacob
1 Jacob
Copying values without constructor
Example-
class Demo{
int id;
String name;
Demo(int i,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Demo s1 = new Demo(1,"Jacob");
Demo s2 = new Demo();
s2.id=s1.id;
s2.name=s1.name;
s1.display();
s2.display();
}
}
Output-
1 Jacob
1 Jacob